<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class EvaluateController extends Controller
{
  
    // 添加评价页面
    // admin/evaluate/add
    public function getSucc()
    {
    	// 显示模板
    	return view('evaluate.succ');

    }
    public function getEval(Request $request)
    {   
        dd($request ->all());
        // $id = $request->input('id');
        return view('evaluate.add');
    }
    public function getAdd(Request $request)
    {   
        // dd($request ->all());
        $id = $request->input('id');
        $data['id'] = $id;
        // dd($data);

        return view('evaluate.add',['data'=>$data]);
    }
    public function postInsert(Request $request){

        $this->validate($request, [
             'des' => 'required'
         ],[
             'username.required' => '评价不能为空',
         ]);
        // 接收数据
        $data = $request->except(['_token','id']);
        // dd($request ->all());

        $id = $request->input('id');
        $dat['id'] = $id;
        // dd($data);
        $re = DB::table('order_goods')->where('goods_id',$dat['id'])->first();
        // dd($re);
        $data['goodsid'] = $re->goods_id;
        $data['orderid'] = $re->order_id;
        // dd($data);
        $res = DB::table('orders')->where('id',$data['orderid'])->first();
        // dd($res);
        $data['userid'] = $res->user_id;
        $der = DB::table('users')->where('id',$data['userid'])->first();
        // dd($der);
        // $data['id'] = $der->id;
        // dd($data);
        // 将数据插入数据库
        $res = DB::table('evaluate')->insert($data);
        if($res){
            //成功 重定向 页面跳转到列表页
            return redirect('/')->with('success','数据添加成功');
        }else{
         //失败 回到添加页
         return back()->with('error','数据插入失败');
        }
    }

    public function getIndex(Request $request)
    {   
        // dd($request ->all());
        // dd(session('id'));
        $data['userid'] = session('id');
        // dd($data);
        $res = DB::table('evaluate')->where('userid',$data['userid'])->get();
        $tmp = [];
        foreach($res as $k=>$v){
            $data['id'] = $v->id;
            $data['des'] = $v->des;
            $data['goodsid'] = $v->goodsid;
            $res1 = DB::table('goods')->where('id',$data['goodsid'])->first();
            // dd($res1);
            $data['proname'] = $res1->proname;
            $data['orderid'] = $v->orderid;
            // dd($data);
            $res2 = DB::table('orders')->where('id',$data['orderid'])->first();
            $data['order_code'] = $res2->order_code;
            $tmp[] = $data;
        }
        // dd($tmp);
        // 解析模板
        return view('evaluate.index',['tmp'=>$tmp]); 
    }

     public function getDelete(Request $request){

        $id=$request->input('id');
        // dd($id);
        $num = DB::table('evaluate')->where('id',$id)->delete();
        echo $num;
        // echo ;
    }
    // // 用户的数据删除
    // // admin/user/delete  get
    // public function getDelete($id){
    //     // 执行删除
    //     $res = DB::table('users')->where('id',$id)->delete();
    //     if($res){
    //         //成功 页面跳转到列表页
    //         return redirect('admin/user/index')->with('success','数据删除成功');
    //     }else{
    //         //失败 跳转到添加页
    //         return back()->with('error','数据删除失败');
    //     }
    // }
}
